$(document).ready(function () {
	map = new AMap.Map('map', {
		mapStyle: 'light'
	});
	pull();
});

MARKERS = {};
CIRCLE = {};

function pull() {
	$.ajax({type:'post',
		url:SITE_PATH+'admin/control/mapPull', success:function (j) {
			try {
				if(j.result > 0) {
					$('#time').text(j.time);
					var ids = {};
					for(var id in MARKERS) {
						ids[id] = true;
					}
					for(var k in j.workers) {
						var worker = j.workers[k];
						if(MARKERS[worker.id]) {
							// --- 如果经纬度相差过大，那么直接移动而不用缓动 ---
							var pos = MARKERS[worker.id].getPosition();
							//if((Math.abs(pos.getLat() - worker.lat) > 1) || (Math.abs(pos.getLng() - worker.lng) > 1)) {
								MARKERS[worker.id].setPosition(new AMap.LngLat(worker.lng, worker.lat));
							//} else {
							//	MARKERS[worker.id].moveTo(new AMap.LngLat(worker.lng, worker.lat), 200);
							//}
							CIRCLE[worker.id].setCenter(new AMap.LngLat(worker.lng, worker.lat));
							ids[worker.id] = null;
							delete(ids[worker.id]);
						} else {
							MARKERS[worker.id] = new AMap.Marker({
								position: new AMap.LngLat(worker.lng, worker.lat),
								map: map
							});
							MARKERS[worker.id].setLabel({
								offset: new AMap.Pixel(9.5, -25),
								content: worker.realname,
							});
							CIRCLE[worker.id] = new AMap.Circle({
								center: new AMap.LngLat(worker.lng, worker.lat),
								strokeColor: '#4699df',
								strokeOpacity: .3,
								strokeWeight: 1,
								fillColor: '#4699df',
								fillOpacity: 0.1,
								map: map
							});
						}
						CIRCLE[worker.id].setRadius(worker.acc);
					}
					for(var id in ids) {
						MARKERS[id].setMap(null);
						MARKERS[id] = null;
						delete(MARKERS[id]);
						CIRCLE[id].setMap(null);
						CIRCLE[id] = null;
						delete(CIRCLE[id]);
					}
				} else
					$('#time').text(j.msg);
				setTimeout('pull()', 1000);
			} catch(e) {
				alert('程序出错，请与技术部联系');
				setTimeout('pull()', 5000);
			}
		}, error: function() {
			$('#time').html('网络连接错误<br>若网络连通将自动恢复');
			setTimeout('pull()', 5000);
		}
	});
}

